package com.kanas.webak.organization.service;

import com.kanas.webak.organization.domain.Calendar;

//import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Singleton;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.kanas.webak.organization.domain.Organization;
import com.kanas.webak.organization.repository.CalendarRepository;
import com.kanas.webak.organization.repository.OrganizationRepository;
import com.kanas.webak.organization.view.Event;

@Service("com.kanas.webak.organization.service.eventservice")
@Singleton
public class EventServiceImpl implements EventService{
	@Autowired
	private CalendarRepository calendarRepository;
	@Autowired 
	private OrganizationRepository organizationRepository;
	
	public CalendarRepository getCalendarRepository() {
		return calendarRepository;
	}

	public void setCalendarRepository(CalendarRepository calendarRepository) {
		this.calendarRepository = calendarRepository;
	}

	public OrganizationRepository getOrganizationRepository() {
		return organizationRepository;
	}

	public void setOrganizationRepository(
			OrganizationRepository organizationRepository) {
		this.organizationRepository = organizationRepository;
	}

	@Override
	@Transactional
	public List<Event> findEventByOrgByMonth(String oid, Date beginDate ,Date endDate) {
		List<Event> events = new ArrayList<Event>();
		java.util.Calendar calUtil = java.util.Calendar.getInstance();
		calUtil.setTime(beginDate);
		java.util.Calendar c = java.util.Calendar.getInstance();
	 	c.setTime(endDate);
		Organization o = null;
		if(oid != null) {
			o = this.organizationRepository.findOne(oid);
		}
		List<Calendar> rules = this.calendarRepository.findByOrganizationAndBeginDateBetweenAndSortNumberNotNullAndRuleFlagOrderBySortNumberDesc(o, calUtil.getTime(), c.getTime(),true);
		List<Calendar> workdays = this.calendarRepository.findByOrganizationAndBeginDateBetweenAndRuleFlagAndWorkdayFlag(o, calUtil.getTime(), c.getTime(),false,true);
		List<Calendar> holidays = this.calendarRepository.findByOrganizationAndBeginDateBetweenAndRuleFlagAndWorkdayFlag(o, calUtil.getTime(), c.getTime(),false,false);
		
		//遍历一个月中的日期
		java.util.Calendar start = java.util.Calendar.getInstance();
		start.setTime(calUtil.getTime());
        java.util.Calendar end = java.util.Calendar.getInstance();
        end.setTime(c.getTime());
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        
        Integer week = null;
        Boolean monday = false;
        Boolean tuesday = false;
        Boolean wednesday = false;
        Boolean thursday = false;
        Boolean friday = false;
        Boolean saturday = false;
        Boolean sunday = false;
        
        Outerloop:
        while(start.compareTo(end) <= 0) {
        	//System.out.println(format.format(start.getTime()));
        	
        	for(Calendar w:workdays) {
        		if(isDateOverlap(start.getTime(),start.getTime(),w.getBeginDate(),w.getEndDate())) {
        			events.add(new Event(w.getAffair(), start.getTime(), start.getTime(), "Workday", w.getId()));
        			start.set(java.util.Calendar.DATE, start.get(java.util.Calendar.DATE) + 1);
        			continue Outerloop;
        		}
        	}
        	
        	for(Calendar h:holidays) {
        		if(isDateOverlap(start.getTime(),start.getTime(),h.getBeginDate(),h.getEndDate())) {
        			events.add(new Event(h.getAffair(), start.getTime(), start.getTime(), "Holiday", h.getId()));
        			start.set(java.util.Calendar.DATE, start.get(java.util.Calendar.DATE) + 1);
        			continue Outerloop;
        		}
        	}
        	
        	for(Calendar r:rules) {
        		if(isDateOverlap(start.getTime(),start.getTime(),r.getBeginDate(),r.getEndDate())) {
        			monday = r.getMonday()==null?false:r.getMonday();
    		        tuesday = r.getTuesday()==null?false:r.getTuesday();
    		        wednesday = r.getWednesday()==null?false:r.getWednesday();
    		        thursday = r.getThursday()==null?false:r.getThursday();
    		        friday = r.getFriday()==null?false:r.getFriday();
    		        saturday =r.getSaturday()==null?false:r.getSaturday();
    		        sunday = r.getSunday()==null?false:r.getSunday();
        			week = start.get(java.util.Calendar.DAY_OF_WEEK);
                    if(week == java.util.Calendar.MONDAY && monday) {
                    	events.add(new Event(r.getAffair(), start.getTime(), start.getTime(), "Workday", r.getId()));
                    	start.set(java.util.Calendar.DATE, start.get(java.util.Calendar.DATE) + 1);
            			continue Outerloop;
                    }
                    if(week == java.util.Calendar.TUESDAY && tuesday) {
                    	events.add(new Event(r.getAffair(), start.getTime(), start.getTime(), "Workday", r.getId()));
                    	start.set(java.util.Calendar.DATE, start.get(java.util.Calendar.DATE) + 1);
            			continue Outerloop;
                    }
                    if(week == java.util.Calendar.WEDNESDAY && wednesday) {
                    	events.add(new Event(r.getAffair(), start.getTime(), start.getTime(),"Workday", r.getId()));
                    	start.set(java.util.Calendar.DATE, start.get(java.util.Calendar.DATE) + 1);
            			continue Outerloop;
                    }	
                    if(week == java.util.Calendar.THURSDAY && thursday) {
                    	events.add(new Event(r.getAffair(), start.getTime(), start.getTime(), "Workday", r.getId()));
                    	start.set(java.util.Calendar.DATE, start.get(java.util.Calendar.DATE) + 1);
            			continue Outerloop;
                    }
                    if(week == java.util.Calendar.FRIDAY && friday) {
                    	events.add(new Event(r.getAffair(), start.getTime(), start.getTime(), "Workday", r.getId()));
                    	start.set(java.util.Calendar.DATE, start.get(java.util.Calendar.DATE) + 1);
            			continue Outerloop;
                    }
                    if(week == java.util.Calendar.SATURDAY && saturday) {
                    	events.add(new Event(r.getAffair(), start.getTime(), start.getTime(), "Workday", r.getId()));
                    	start.set(java.util.Calendar.DATE, start.get(java.util.Calendar.DATE) + 1);
            			continue Outerloop;
                    }
                    if(week == java.util.Calendar.SUNDAY && sunday) {
                    	events.add(new Event(r.getAffair(), start.getTime(), start.getTime(), "Workday", r.getId()));
                    	start.set(java.util.Calendar.DATE, start.get(java.util.Calendar.DATE) + 1);
            			continue Outerloop;
                    }
        		}
        	}
        	//events.add(new Event("无安排", start.getTime(), start.getTime(), "Holiday"));
        	start.set(java.util.Calendar.DATE, start.get(java.util.Calendar.DATE) + 1);
        }
		
		return events;
	}
	
	private Boolean isDateOverlap(Date date1Begin, Date date1End, Date date2Begin, Date date2End) {
		Boolean flag = true;
		Integer firstFlag = date1End.compareTo(date2Begin);
		Integer secondFlag = date2End.compareTo(date1Begin);
		
		if(firstFlag < 0 || secondFlag <0 ) {
			flag = false;
		}
		
		return flag;
		
	}
}
